{include file="common/header" /}
<div class="page-body">
    <div class="container-xl">
        <div class="row row-cards">

          <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col">
                            <form method="get" action="">
                                <div class="row g-2">
                                    <div class="col-auto">
                                    <input type="text" value="{if isset($queryFieldWhere)}{$queryFieldWhere}{/if}" name="queryField" class="form-control" placeholder="请输入编号或产品型号">
                                    </div>
                                    <div class="col-auto">
                                        <select name="status" class="form-select">
                                            <option value="" {if !isset($statusWhere) || $statusWhere == ''} selected{/if}>全部</option>
                                            <option value="0" {if isset($statusWhere) && $statusWhere == 0} selected{/if}>待审核</option>
                                            <option value="1" {if isset($statusWhere) && $statusWhere == 1} selected{/if}>通过</option>
                                            <option value="2" {if isset($statusWhere) && $statusWhere == 2} selected{/if}>未通过</option>
                                        </select>
                                    </div>
                                    <div class="col-auto">
                                    <button type="submit"  class="btn btn-icon" aria-label="Button">
                                        <!-- Download SVG icon from http://tabler-icons.io/i/search -->
                                        <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0"></path><path d="M21 21l-6 -6"></path></svg>
                                    </button>
                                    </div>
                                    <div class="col">
                                        <a href="/index/home/fileList" class="btn">
                                            重置
                                        </a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

            <div class="col-md-12 offset-md-0">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">产品列表</h4>
                        {if $userInfo.role == 1}
                        <div class="card-actions">
                          <a href="#" data-bs-toggle="modal" data-bs-target="#addProductModal" class="btn btn-primary">
                          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 5l0 14"></path><path d="M5 12l14 0"></path></svg>
                          添加产品
                          </a>
                        </div>
                        {/if}
                    </div>
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table">
                            <thead>
                              <tr>
                                <th>编号</th>
                                <th>产品型号</th>
                                <th>固晶</th>
                                <th>焊线</th>
                                <th>测试</th>
                                <th>产品规范</th>
                                <th>客户源文件</th>
                                <th>审核状态</th>
                                <th>操作</th>
                              </tr>
                            </thead>
                            <tbody>
                                {foreach $productList as $key=>$vo } 
                                  <tr>
                                    <td class="text-secondary">
                                        {$vo.product_no}
                                    </td>
                                    <td class="text-secondary">
                                        {$vo.model}
                                    </td>
                                    <td class="text-secondary">
                                      {$vo.gj_num}
                                    </td>
                                    <td class="text-secondary">
                                      {$vo.hx_num}
                                    </td>
                                    <td class="text-secondary">
                                      {$vo.test_num}
                                    </td>
                                    <td class="text-secondary">
                                      <a href="/index/home/getFile?url={$vo.standard_file_url}&name={$vo.standard_file_name}">
                                        {$vo.standard_file_name}
                                      </a>
                                    </td>
                                    <td class="text-secondary">
                                      <a href="/index/home/getFile?url={$vo.source_file_url}&name={$vo.source_file_name}">
                                        {$vo.source_file_name}
                                      </a>
                                    </td>
                                    <td class="text-secondary">
                                        {if $vo.status == 1}
                                        <span class="badge bg-success-lt">通过</span>
                                        {/if}
                                        {if $vo.status ==2}
                                        <span class="badge bg-danger-lt">未通过</span>
                                        {/if}
                                        {if $vo.status == 0}
                                        <span class="badge bg-warning-lt">待审核</span>
                                        {/if}
                                    </td>
                                    <td class="text-secondary">
                                        {if $userInfo.role == 2 && $vo.status == 0}
                                        <span onclick="setProductId('{$vo.id}')" data-bs-toggle="modal" data-bs-target="#approvalProductModal" class="badge bg-primary-lt">审核</span>
                                        {/if}
                                        {if $userInfo.role == 3 && $vo.status == 1}
                                        <span onclick="setProductId('{$vo.id}')" data-bs-toggle="modal" data-bs-target="#approvalIssueModal" class="badge bg-primary-lt">申请下发</span>
                                        {/if}
                                        {if $userInfo.role == 1}
                                        <span onclick="setProductIdByEdit('{$vo.id}','{$vo.product_no}','{$vo.model}')" data-bs-toggle="modal" data-bs-target="#editProductModal" class="badge bg-primary-lt">修改</span>
                                        {/if}
                                        {if $userInfo.role == 1}
                                        <span onclick="setProductId('{$vo.id}')" data-bs-toggle="modal" data-bs-target="#delProductModal" class="badge bg-lt">删除</span>
                                        {/if}
                                    </td>
                                  </tr>
                                {/foreach}
                            </tbody>
                          </table>
                    </div>
                  </div>
            </div>
        </div>
    </div>
</div>

  <!-- 添加产品模态框 -->
  <div class="modal modal-blur fade" id="addProductModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">添加产品</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">

          <div class="mb-3">
            <label class="form-label">产品编号</label>
            <div class="input-icon mb-3">
              <input id="product_no" type="text" class="form-control" placeholder="请输入产品编号">
            </div>
          </div>
          <div class="mb-3">
            <label class="form-label">产品型号</label>
            <div class="input-icon mb-3">
              <input id="model" type="text" class="form-control" placeholder="请输入产品型号">
            </div>
          </div>

          <div class="mb-3">
            <label class="form-label">产品规范</label>
            <input id="standard_file" type="file" class="form-control">
          </div>

          <div class="mb-3">
            <label class="form-label">客户源文件</label>
            <input id="source_file" type="file" class="form-control">
          </div>

        </div>
        <div class="modal-footer">
          <button type="button" class="btn me-auto" data-bs-dismiss="modal">取消</button>
          <button type="button" class="btn btn-primary" onclick="addProduct()">添加</button>
        </div>
      </div>
    </div>
  </div>
  <!-- 添加产品模态框结束 -->

  <!-- 修改产品模态框 -->
  <div class="modal modal-blur fade" id="editProductModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">修改产品</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">

          <div class="mb-3">
            <label class="form-label">产品编号</label>
            <div class="input-icon mb-3">
              <input id="product_no_edit" type="text" class="form-control" placeholder="请输入产品编号">
            </div>
          </div>
          <div class="mb-3">
            <label class="form-label">产品型号</label>
            <div class="input-icon mb-3">
              <input id="model_edit" type="text" class="form-control" placeholder="请输入产品型号">
            </div>
          </div>

          <div class="mb-3">
            <label class="form-label">产品规范</label>
            <input id="standard_file_edit" type="file" class="form-control">
          </div>

          <div class="mb-3">
            <label class="form-label">客户源文件</label>
            <input id="source_file_edit" type="file" class="form-control">
          </div>

        </div>
        <div class="modal-footer">
          <button type="button" class="btn me-auto" data-bs-dismiss="modal">取消</button>
          <button type="button" class="btn btn-primary" onclick="editProduct()">修改</button>
        </div>
      </div>
    </div>
  </div>
  <!-- 修改产品模态框结束 -->

  <!-- 产品审核模态框 -->
  <div class="modal modal-blur fade" id="approvalProductModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">产品审核</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <div class="mb-3">
            <label class="form-label">拒绝原因</label>
            <textarea id="approvalRefuseMsg" placeholder="请输入审核拒绝原因" class="form-control" rows="5"></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" onclick="approval(2)">拒绝</button>
          <button type="button" class="btn btn-primary" onclick="approval(1)">通过</button>
        </div>
      </div>
    </div>
  </div>
  <!-- 产品审核模态框结束 -->

  
  <!-- 申请下发模态框模态框 -->
  <div class="modal modal-blur fade" id="approvalIssueModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">申请下发</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <div class="mb-3">
            <label class="form-label">申请数量</label>
            <div class="input-icon mb-3">
              <input id="num" type="text" class="form-control" placeholder="请输入申请数量">
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn me-auto" data-bs-dismiss="modal">取消</button>
          <button type="button" class="btn btn-primary" onclick="approvalIssue()">申请</button>
        </div>
      </div>
    </div>
  </div>
  <!-- 申请下发模态框结束 -->

  <!-- 产品删除模态框模态框 -->
  <div class="modal modal-blur fade" id="delProductModal" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-sm modal-dialog-centered" role="document">
      <div class="modal-content">
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        <div class="modal-status bg-danger"></div>
        <div class="modal-body text-center py-4">
          <!-- Download SVG icon from http://tabler-icons.io/i/alert-triangle -->
          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon mb-2 text-danger icon-lg"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 9v4"></path><path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z"></path><path d="M12 16h.01"></path></svg>
          <h3>确认删除吗?</h3>
          <div class="text-secondary">您确认要删除此产品吗? 删除后将不可恢复.</div>
        </div>
        <div class="modal-footer">
          <div class="w-100">
            <div class="row">
              <div class="col"><a href="#" class="btn w-100" data-bs-dismiss="modal">
                  取消
                </a></div>
              <div onclick="deleteProduct()" class="col"><a href="#" class="btn btn-danger w-100">
                  删除
                </a></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- 产品删除模态框模态框结束 -->
{include file="common/footer" /}


<script>
var standard_file;
var source_file;
var standard_file_edit;
var source_file_edit;
$(document).ready(function() {
    $('#standard_file').change(function(){
        var file = $(this)[0].files[0];
        if (file) {
            standard_file = file;
            var ext = file.name.substring(file.name.lastIndexOf('.') + 1);
        } else {
            standard_file = null;
        }
    });

    $('#source_file').change(function(){
        var file = $(this)[0].files[0];
        if (file) {
            source_file = file;
            var ext = file.name.substring(file.name.lastIndexOf('.') + 1);
        } else {
            source_file = null;
        }
    });

    $('#standard_file_edit').change(function(){
        var file = $(this)[0].files[0];
        if (file) {
          standard_file_edit = file;
            var ext = file.name.substring(file.name.lastIndexOf('.') + 1);
        } else {
          standard_file_edit = null;
        }
    });

    $('#source_file_edit').change(function(){
        var file = $(this)[0].files[0];
        if (file) {
          source_file_edit = file;
            var ext = file.name.substring(file.name.lastIndexOf('.') + 1);
        } else {
          source_file_edit = null;
        }
    });
});

//当前操作产品的id
var productId;
function setProductId(id){
    productId = id;
}

function setProductIdByEdit(id,product_no,model){
  setProductId(id)
  $('#product_no_edit').val(product_no)
  $('#model_edit').val(model)
}

function addProduct(){
    var product_no = $('#product_no').val();
    var model = $('#model').val();
    if(product_no == ''){layer.msg("请输入产品编号"); return;}
    if(model == ''){layer.msg("请输入产品型号"); return;}
    if(standard_file == '' || standard_file == null){layer.msg("请选择产品规范文件"); return;}
    if(source_file == '' || source_file == null){layer.msg("请选择客户源文件"); return;}
    formdata = new FormData();
    formdata.append('product_no',product_no)
    formdata.append('model',model)
    formdata.append('standard_file',standard_file)
    formdata.append('source_file',source_file)
    axios.post('/index/product/add',formdata)
    .then(function (res) {
        layer.msg(res.data.msg)
        if(res.data.code == 200){
            window.location.href = ""
        }
    })
    .catch(function (error) {
        console.log(error);
    });
}

function editProduct(){
    var product_no = $('#product_no_edit').val();
    var model = $('#model_edit').val();
    if(product_no == ''){layer.msg("请输入产品编号"); return;}
    if(model == ''){layer.msg("请输入产品型号"); return;}
    if(standard_file_edit == '' || standard_file_edit == null){layer.msg("请选择产品规范文件"); return;}
    if(source_file_edit == '' || source_file_edit == null){layer.msg("请选择客户源文件"); return;}
    formdata = new FormData();
    formdata.append('id',productId)
    formdata.append('product_no',product_no)
    formdata.append('model',model)
    formdata.append('standard_file',standard_file_edit)
    formdata.append('source_file',source_file_edit)
    axios.post('/index/product/edit',formdata)
    .then(function (res) {
        layer.msg(res.data.msg)
        if(res.data.code == 200){
            window.location.href = ""
        }
    })
    .catch(function (error) {
        console.log(error);
    });
}

function approval(type){
    if(productId == '' || productId == null){
        layer.msg('操作的产品id不存在');
        return;
    }
    var approvalRefuseMsg = $('#approvalRefuseMsg').val();
    axios.post('/index/product/approval',{
        id:productId,
        type:type,
        approvalRefuseMsg:approvalRefuseMsg
    })
    .then(function (res) {
        layer.msg(res.data.msg)
        if(res.data.code == 200){
            window.location.href = ""
        }
    })
    .catch(function (error) {
        console.log(error);
    });
}

function approvalIssue(){
    var num = $('#num').val();
    axios.post('/index/issue/approval',{
        id:productId,
        num:num
    })
    .then(function (res) {
        layer.alert(res.data.msg)
    })
    .catch(function (error) {
        console.log(error);
    });
}

function deleteProduct(){
  axios.post('/index/product/delete',{
      id:productId
  })
  .then(function (res) {
      layer.msg(res.data.msg)
      if(res.data.code == 200){
        window.location.href = ""
      }
  })
  .catch(function (error) {
      console.log(error);
  });
}
</script>

